var Net = {
	className: 'Net',

	Client: function(debugOn)
	{
		this.host = 'localhost';
		this.port = '80';
		this.socket = null;

		this.debugOn = debugOn

		this.queue = [];

		this.busy = false

		this._connected = false

		this._events = {
			_onOpenEvents: [],
			_onCloseEvents: [],
			_onMessageEvents: [],
			_onWriteEvents: []
		}

		this.debug = function(s)
		{
			if(this.debugOn)
			{
				console.log(s);
			}
		}

		this.registerEvent = function(bunch, event)
		{
			bunch.push(event);
		}

		this.triggerEvents = function(bunch, data)
		{
			for(event in bunch)
			{
				bunch[event](this, data);
			}
		}

		this.setHost = function(host)
		{
			this.host = host;

			return this;
		}
		this.setPort = function(port)
		{
			this.port = port;

			return this;
		}
		this.setHostPort = function(host, port)
		{
			this.setHost(host);
			this.setPort(port);

			return this;
		}
		this.connect = function()
		{
			this.socket = new WebSocket('ws://'+this.host+':31337/');

//			var __events = this._events;

			var obj = this;

			this.socket.onopen = function(){
				obj.debug('Connected to websocket server');
				obj._connected = true;
				obj.triggerEvents(obj._events._onOpenEvents)
			};
			this.socket.onclose = function(){
				obj.debug('Connection closed');
				obj._connected = false;
				obj.triggerEvents(obj._events._onCloseEvents)
			};
			this.socket.onmessage = function(data){
				obj.debug('Got `'+data.data+'`');
//				s = '';
//				for(i in data.data)
//				{
//					s += '*' + data.data[i] + '*\n';
//				}
//				alert(s);
				obj.triggerEvents(obj._events._onMessageEvents, JSON.parse(data.data))
			};

			return this;
		}

		this._onOpen = function()
		{
			this.triggerEvents(this._events._onOpenEvents);
		}

		this._onClose = function()
		{
			this.triggerEvents(this._events._onCloseEvents);
		}

		this._onMessage = function(data)
		{
			this.triggerEvents(this._events._onMessageEvents, data);
		}

		this._onWrite = function(data)
		{
			this.triggerEvents(this._events._onWriteEvents, data);
		}

		this.connected = function()
		{
			return this._connected;
		}

		this.onOpen = function(fn)
		{
			this.registerEvent(this._events._onOpenEvents, fn);

			return this;
		}
		this.onClose = function(fn)
		{
			this.registerEvent(this._events._onCloseEvents, fn);

			return this;
		}

		this.onWrite = function(fn)
		{
			this.registerEvent(this._events._onWriteEvents, fn);

			return this;
		}

/*		this._onOpen = function()
		{
			this._connected = true;
			alert('Connected!')
			this.send('asd')
//			this.onOpen();
		}

		this._onClose = function()
		{
			this._connected = true;
			alert('Disconnected.')
//			this.onClose();
		}*/
		
		this.onMessage = function(fn, data)
		{
			// TODO: Split incoming JSONs into parts if there are few of them!
			// This may later be the pain in the ass!
//			alert(e.data);
			this.registerEvent(this._events._onMessageEvents, fn);

			return this;
		}

		this.send = function(data)
		{
			this.triggerEvents(this._events._onWriteEvents, data)

			json = JSON.stringify(data);

			(data.a != 'ping') && this.debug('Senging `'+json+'`')

			this.socket.send(json);
		}

		this.get = function()
		{
//			this.registerEvents(this._events._onGetEvents, fn);
		}

		this.foo = function(data)
		{
			alert('bar');
		}
	},

	Pinger: function(client, interval)
	{
		this.callback = null;

		this.start = function()
		{
			this.setInterval();
		}

		this.setInterval = function()
		{
			var obj = this;
			this.callback = window.setInterval(function(){
				if(obj.client.connected())
				{
//					debug.add('Timeout');
					obj.client.send({'a':'ping','fps':GUI.fps});
				}
			}, interval);
		}

		this.reset = function()
		{
			window.clearInterval(this.callback);
			this.setInterval();
		}

		this.stop = function()
		{
			if(this.callback)
			{
				window.removeInterval(this.callback);
			}
		}

		this.client = client;
		this.interval = interval;
		this.start();
	},

	Reconnector: function(client, interval)
	{
		this.lock = false;

		this.start = function()
		{
			if(!this.callback)
			{
				var obj = this;
				this.callback = window.setInterval(function(){
					if(!obj.client.connected())
					{
//						if(this.lock)
//						{
//							return;
//						}
//						this.lock = true;
//						debug.warning('No connection, trying to reconnect...');
//						obj.client.connect();
//						this.lock = false;
					}
				}, interval);
			}
		}

		this.client = client;

		this.start();
	}
};
